package com.liang.index334.domain;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.util.StringUtils;
import lombok.Data;

import java.math.BigDecimal;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liangyehao
 * @version 1.0
 * @date 2021/4/30 22:48
 * @content
 */
@Data
public class AgentResult {

    @ExcelProperty("门店")
    private String stores;
    @ExcelProperty("店组")
    private String storesGroup;
    @ExcelProperty("姓名")
    private String agentName;
    @ExcelProperty("经纪类型")
    private String brokerageType;
    @ExcelProperty({"考核项1","本月扫码量"})
    private Integer codeAmount;
    @ExcelProperty({"考核项1","月均码上有客（月目标4组）"})
    private BigDecimal codeGuest;
    @ExcelProperty({"考核项1","是否预警关黑"})
    private String codeFlunk;
    @ExcelProperty({"考核项2","拉新委托（月目标4组）"})
    private Integer pullNewAmount;
    @ExcelProperty({"考核项2","是否预警关黑"})
    private String pullNewFlunk;
    @ExcelProperty({"考核项3","VR带看（月目标12组）"})
    private Integer vrAmount = 0;
    @ExcelProperty({"考核项3","是否预警关黑"})
    private String vrFlunk = "预警关黑";
    @ExcelProperty("总计不达标")
    private long flunkAmount;

    public static List<AgentResult> build(List<CodeResult> codeResults,
                                          List<NewResult> newResults,
                                          List<VrResult> vrResults){
        List<AgentResult> agentResults = new ArrayList<>();

        for (CodeResult codeResult : codeResults) {

            List<NewResult> newCollect = newResults.stream()
                    .filter(data -> codeResult.getAgentName().equals(data.getAgentName()))
                    .collect(Collectors.toList());

            List<VrResult> vrCollect = vrResults.stream()
                    .filter(data -> codeResult.getAgentName().equals(data.getAgentName()))
                    .collect(Collectors.toList());

            AgentResult agentResult = new AgentResult();
            // 扫码
            agentResult.setStores(codeResult.getStores());
            agentResult.setStoresGroup(codeResult.getStoresGroup());
            agentResult.setAgentName(codeResult.getAgentName());
            agentResult.setCodeAmount(codeResult.getCodeAmount());
            agentResult.setCodeGuest(codeResult.getCodeGuest());
            agentResult.setCodeFlunk(codeResult.getCodeFlunk());
            // 拉新
            if (newCollect.size()>0) {
                NewResult newResult = newCollect.get(0);
                String type = newResult.getJobs();
                agentResult.setBrokerageType(type);
                agentResult.setPullNewAmount(newResult.getNewAmount());
                agentResult.setPullNewFlunk(newResult.getNewFlunk());
            }
            // Vr
            if (vrCollect.size()>0) {
                VrResult vrResult = vrCollect.get(0);
                agentResult.setVrAmount(vrResult.getVrAmount());
                agentResult.setVrFlunk(vrResult.getVrFlunk());
            }

            // 处理非买卖的vr数据
            if (agentResult.getBrokerageType()==null ||
                    !"买卖经纪人".equals(agentResult.getBrokerageType())) {
                agentResult.setVrFlunk("已达标");
            }


            // 总计不达标
            long count = Stream.of(
                    agentResult.getCodeFlunk(),
                    agentResult.getPullNewFlunk(),
                    agentResult.getVrFlunk()
            ).filter(flunk -> !"已达标".equals(flunk)).count();

            agentResult.setFlunkAmount(count);

            agentResults.add(agentResult);

        }

        return agentResults.stream()
                .sorted(Comparator.comparing(AgentResult::getStores))
                .collect(Collectors.toList());
    }

}
